﻿Imports System.Drawing.Drawing2D
Imports System.Drawing.Design
Imports System.ComponentModel
Imports System.Windows.Forms.Design
Imports FixSoft.Tools.Common
Imports FixSoft.Tools.Common.CommonTools

Friend Class BSaver
    Friend Property Parent As DropdownColorBlender
    Dim ForeColorBlendv As GradientBlend
    Public Property ColorBlend As GradientBlend
        Set(ByVal value As GradientBlend)
            Dim IsChanged As Boolean = Not value Is ForeColorBlendv
            ForeColorBlendv = value
            If IsChanged Then
                Parent.UpdateSource()
            End If
        End Set
        Get
            Return ForeColorBlendv
        End Get
    End Property
End Class
#Region "BlendTypeEditor - UITypeEditor"

#Region "cBlendItems"

Public Class GradientBlend

    Sub New()

    End Sub

    Sub New(ByVal Color As Color(), ByVal Pt As Single())
        iColor = Color
        iPoint = Pt
    End Sub

    Private _iColor As Color()
    <Description("The Color for the Point"), _
       Category("Appearance")> _
    Public Property iColor() As Color()
        Get
            Return _iColor
        End Get
        Set(ByVal value As Color())
            _iColor = value
        End Set
    End Property

    Private _iPoint As Single()
    <Description("The Color for the Point"), _
       Category("Appearance")> _
    Public Property iPoint() As Single()
        Get
            Return _iPoint
        End Get
        Set(ByVal value As Single())
            _iPoint = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return "BlendItems"
    End Function

    Public Shared Sub DrawGradient(ByVal Graphics As Graphics, ByVal GradientBlend As GradientBlend, ByVal Bounds As Rectangle, ByVal Options As GradientOptions)
        If Bounds.Width = 0 Then
            Bounds.Width = 1
        End If
        If Bounds.Height = 0 Then
            Bounds.Height = 1
        End If
        Dim gr As Graphics = Graphics
        Dim brush As LinearGradientBrush
        If Options.UseGradientMode = True Then
            brush = New LinearGradientBrush(Bounds, Color.White, Color.White, Options.GradientMode)
        Else
            brush = New LinearGradientBrush(Bounds, Color.White, Color.White, Options.GradientAngel)
        End If
        Dim cb As New ColorBlend
        cb.Colors = EnableBlends(GradientBlend.iColor, Options.Enabled)
        cb.Positions = GradientBlend.iPoint

        If Options.GradientType = GradientType.Radial Then
            '--------------------Radial-----------------------
            Dim gp As New GraphicsPath
            gp.AddEllipse(Bounds)
            Dim rg As New PathGradientBrush(gp)
            rg.CenterPoint = New PointF(Bounds.Width / 2, Bounds.Height / 2)
            rg.InterpolationColors = cb
            '--------------------Radial-----------------------
            gr.FillRectangle(New SolidBrush(cb.Colors(0)), Bounds)
            gr.FillRectangle(rg, Bounds)
        Else
            brush.InterpolationColors = cb
            'If SetBlend = True Then
            '    brush.SetBlendTriangularShape(blendv)
            '   End If
            gr.FillRectangle(brush, Bounds)
        End If

        brush.Dispose()
    End Sub

    ''' <summary>
    ''' Convert ColorBlend to grayscale if Disabled else return origional ColorBlend.
    ''' </summary>
    ''' <param name="ColorIn">ColorBlend to Check</param>
    ''' <param name="Enabled">A value that indicates whether the control is enabled.</param>
    Private Shared Function EnableBlends(ByVal ColorIn As Color(), ByVal Enabled As Boolean) As Color()

        If Enabled Then
            Return ColorIn
        Else
            Dim tcolor As New List(Of Color)
            For Each c As Color In ColorIn
                Dim gray As Integer = CInt(c.R * 0.3 + c.G * 0.59 + c.B * 0.11)
                tcolor.Add(Color.FromArgb(c.A, gray, gray, gray))
            Next
            Return tcolor.ToArray
        End If
    End Function
End Class

Public Class GradientOptions
    ''' <summary>
    ''' Gets or sets window gradient mode.
    ''' if you want better option , use gradientAngel.
    ''' </summary>
    <CategoryAttribute("Appearance"), DefaultValue(GetType(LinearGradientMode), "Vertical"),
   DescriptionAttribute("set this or set gradient angel to change gradient angel.")>
    Public Property GradientMode As LinearGradientMode

    ''' <summary>
    ''' Gets or sets notify dorm gradient angel.
    ''' </summary>
    <CategoryAttribute("Appearance"), DefaultValue(GetType(Single), "90"),
   DescriptionAttribute("set this or set gradient mode to change gradient angel.")>
    Public Property GradientAngel As Single

    Public Property UseGradientMode As Boolean
    Public Property GradientType As GradientType
    Public Property Enabled As Boolean
End Class
Public Enum GradientType
    Linear
    Radial
End Enum
#End Region 'cBlendItems

Public Class BlendTypeEditor
    Inherits UITypeEditor
    ''' <summary>
    ''' Overrides this to change the name of property which is a GradientBlend.
    ''' </summary>
    Public Overridable ReadOnly Property PropertyName As String
        Get
            Return "BackgroundGradient"
        End Get
    End Property

    Public Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
        If Not context Is Nothing Then
            Return UITypeEditorEditStyle.DropDown
        End If
        Return (MyBase.GetEditStyle(context))
    End Function

    Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
        On Error Resume Next
        If (Not context Is Nothing) And (Not provider Is Nothing) Then
            ' Access the property browser's UI display service, IWindowsFormsEditorService
            Dim editorService As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            If Not editorService Is Nothing Then
                ' Create an instance of the UI editor, passing a reference to the editor service
                Dim dropDownEditor As DropdownColorBlender = New DropdownColorBlender(editorService)
                dropDownEditor.Editor = Me
                dropDownEditor.Context = context
                Dim Instance = context.Instance
                dropDownEditor.OldGradient = CallByName(Instance, PropertyName, CallType.Get)
                'Update The Sample with the Current Instance's Properties
                With dropDownEditor
                    .LoadABlend(dropDownEditor.OldGradient) '(New StringMember() With {.MemberName = PropertyName, .ParentClass = Instance}.Value)
                End With
                ' Display the UI editor
                editorService.DropDownControl(dropDownEditor)
                ' Return the new property value from the editor
                If dropDownEditor.GradientSaver.ColorBlend IsNot Nothing Then
                    Return dropDownEditor.GradientSaver.ColorBlend
                Else
                    Return dropDownEditor.OldGradient
                End If
            End If
        End If
        Return MyBase.EditValue(context, provider, value)
    End Function

    ' Indicate that we draw values in the Properties window.
    Public Overrides Function GetPaintValueSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
        Return True
    End Function

    ' Draw a BorderStyles value.
    Public Overrides Sub PaintValue(ByVal e As System.Drawing.Design.PaintValueEventArgs)
        ' Erase the area.
        e.Graphics.FillRectangle(Brushes.White, e.Bounds)

        ' Draw the sample.
        Dim cblnd As GradientBlend = DirectCast(e.Value, GradientBlend)
        Using br As LinearGradientBrush = New LinearGradientBrush(e.Bounds, Color.Black, Color.Black, LinearGradientMode.Horizontal)
            Dim cb As New ColorBlend
            cb.Colors = cblnd.iColor
            cb.Positions = cblnd.iPoint
            br.InterpolationColors = cb
            e.Graphics.FillRectangle(br, e.Bounds)
        End Using
    End Sub
End Class
#End Region 'BlendTypeEditor - UITypeEdito